package com.kelvin.oocl.percent;


public class FindPercent {
	public static void main(String[] args) {
		try {
			System.out.println(new FindPercent().randowPercent(10,50,0.8,20));
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}
	
	public double randowPercent(int start,int end,double percent,int value) throws Exception{
		if(start<=0 || end<=0){
			throw new Exception("start and end must be greater than 0!");
		}
		if(start>=end){
			throw new Exception("start must be less than end!");
		}
		if(value<start || value>end){
			throw new Exception("value must be between start and end!");
		}
		
		int n = end-start+1;
		double per = (n*percent-1)/(n-1); //计算需要产生的额外概率
		int m = 1000000;
		int sec = 1000;
		int count = 0;
		for(int i=0;i<=m;i++){
			int rn = randowNumber(start, end);
			if(rn==5){
				count++;
			}
			int other = randowNumber(1, sec);
			if(other<=(int)sec*per){
				rn = 5;
				count++;
			}
		}
		
		double a= ((double)count)/((double)m);
		return a;
	}
	
	public int randowNumber(int start,int end) throws Exception{
		if(start>=end){
			throw new Exception("start must be less than end");
		}
		return (int)(Math.random()*(end-start)+start);
	}
}
